import Cesium from '../../Ces/Cesium'
import WeDataParserBase from '../../Display/WeDataParserBase'
import M4ParseBin from './M4ParseBin'
import M4ParseText from './M4ParseText'

class M4Parser extends WeDataParserBase {
  requestImage(url, getUrlCallback) {
    if (getUrlCallback) url = getUrlCallback(url) + ''
    return Cesium.Resource.fetchArrayBuffer(url).then(arrayBuffer => {
      const magic = Cesium.getStringFromTypedArray(new Uint8Array(arrayBuffer, 0, 7))
      if (magic === 'diamond') {
        const decoder = new TextDecoder("utf-8")
        const text = decoder.decode(arrayBuffer)
        return Promise.resolve(M4ParseText(text))
      } else if (magic.slice(0, 4) === 'mdfs') {
        return Promise.resolve(M4ParseBin(arrayBuffer))
      }
    })
  }
}




export default M4Parser
